package com.atguigu.ftp.controller;

import com.atguigu.common.R;
import com.atguigu.ftp.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author panghl
 * @Date 2021/1/6 10:32
 * @Description TODO
 **/
@Api(tags = {"文件管理"})
@RestController
@RequestMapping("/edufile/local")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation(value = "單個文件上传")
    @PostMapping(value = "/upload-file")
    public R uploadFile(@RequestParam(value="file",required = true) MultipartFile file){
        String url = fileService.uploadFile(file);
        return R.ok().data("url",url);
    }

    @ApiOperation(value = "多文件MultipartFile上传")
//    @ApiImplicitParams({@ApiImplicitParam(name = "file[]", value = "文件流对象,接收数组格式", required = true,paramType = "form",dataType = "file",allowMultiple = true)})
    @ApiImplicitParams({@ApiImplicitParam(name = "file[]", value = "文件流对象,接收数组格式", required = true,dataType = "MultipartFile",allowMultiple = true)})
    @PostMapping(value = "/upload-files")
    public R uploadFile(@RequestParam(value="file[]",required = true) MultipartFile[] files){
        List<String> urls = new ArrayList<>();
        try {
            urls = fileService.uploadFiles(files);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.ok().data("urls",urls);
    }

}
